<template>
	<view class="home-wrapper">

		<view style="background-color: #FFFFFF;height: 180px;">
			<swiper class="swiper" :indicator-dots="true" :autoplay="true" :interval="2000" :duration="500">
				<swiper-item v-for="(item,index) in bannerList">
					<image class="img" :src="item.image"></image>
				</swiper-item>
			</swiper>
		</view>
		<!-- <view class="home-header"> -->
			<view class="active">
				<ms-tabs :list="tabList" v-model="active2" itemColor="#FF5F49" lineColor="#FFFFFF" :lineAnimated="false"></ms-tabs>

			<view class="home-fill">
			</view>
			<view class="fill-img">
				<image src="/static/images/shaixuan.png" style="width: 17px;height: 17px; margin: auto;" @click="hanldHomefiltr()"></image>
			</view>
		</view>
		<view class="home"  v-for="(item,index) in Activitylist" :key="index" @click="toDetails(item)" >
			<view class="home-img">
				<image :src="item.image"></image>
			</view>
			<view class="home-list">
				<image src="../../static/images/yijieshu.png" class="home-stus" v-if="time>item.end_time"></image>
				<image src="../../static/images/wei_baom.png" class="home-stus" v-if="time<item.start_time"></image>
				<image src="../../static/images/baoming_zhong.png" class="home-stus" v-if="time>item.start_time&&time<item.end_time"></image>
				<view style="font-size: 15px;color: #333333;font-weight: bold;">{{item.title}}</view>
				<view class="home-book">{{item.type}}</view>
				<view style="color: #FF5F49;" v-if="item.end_time<time">报名已结束</view>
				<view style="color: #FF5F49;" v-else>报名截止{{item.end_date}}</view>
				<view class="home-add">
					<view style="font-size: 16px;color: #FF5F49;">
						<text style="font-size: 12px;">￥</text>{{item.price}}
					</view>
					<view style="color: #999999;font-size: 12px;display: flex;flex-direction: row;align-items: center;    width: 100%;
			 justify-content: space-around;width: 70%;">
						<view style="flex:1;padding-left:10px">{{item.join_count}}人已报名</view>
						<view class="home-photo">
							<image :src="item.avatar" v-for="(item,index) in item.join_list"></image>
			
						</view>
					</view>
				</view>
			</view>
		</view>
		<uni-load-more
		:status="loadStatus"
		></uni-load-more>
		<!--筛选弹框-->
		<view class="screen" v-if="filter">
			<view style="padding: 0px 17px 100px 17px; overflow-y: scroll;">
				<view class="screen-item">
					<view class="screen-title">排序</view>
					<view class="screen-text">
						<view class="screen-con" v-for="(item,index) in sortType" @click="hanldsortType(index)" v-bind:class="[index==sort_stic?'typeStyle':'']">{{item}}</view>
					</view>
				</view>
				<view class="screen-item">
					<view class="screen-title">费用</view>
					<view class="screen-text" >
						<view class="screen-con" v-for="(item,index) in feeList" @click="hanldfee(index)" v-bind:class="[index==fee_stic?'typeStyle':'']">{{item}}</view>
					</view>
				</view>
				<view class="screen-item">
					<view class="screen-title">状态</view>
					<view class="screen-text">
						<view class="screen-con" v-for="(item,index) in stuasList" @click="hanldstatus(index)"  v-bind:class="[index===sta_stic?'typeStyle':'']">{{item}}</view>
					</view>
				</view>
				<view class="screen-item">
					<view class="screen-title">分类</view>
					<view class="screen-text">
						<view class="screen-con" v-for="(item,index) in classifyList" @click="hanldClassify(item,index)"  v-bind:class="[item.id==clss_stic?'typeStyle':'']">{{item.name}}</view>
					</view>
				</view>
			</view>
			<view class="screen-but">
				<view class="search-no" @click="hanldNo()">
					重置
				</view>
				<view class="search-ok" @click="hanldOk()">
					确认
				</view>
			</view>

		</view>
		<view class="screen-bg" v-if="filter" @click="hanldHomeClos()"></view>
		
		<!--授权弹框-->
		<view class="search-body">
			<view class="dialog-container" v-if="tokenShow">
				<view class="dialog-mask"></view>
				<view class="dialog-info">
					<view class="dialog-title">您还未授权</view>
					<view class="dialog-content">请先授权再进行操作</view>
					<view class="dialog-footer">
						<button @click="dontLogin()" class="dialog-btn">暂不登录</button>
						<button @getuserinfo="getLogin" open-type="getUserInfo" class="dialog-btn" style="background-color:#FF5F49;color: #fff;">立即授权</button>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import uniLoadMore from "@/components/uni-load-more/uni-load-more.vue"
	//
	import msTabs from '@/components/ms-tabs/ms-tabs.vue'
	export default {
		data() {
			return {
				time:'',//当前时间
				loadStatus: 'more',
				page: 1,
				 tabList: [{
				                title: '一月'
				            }, {
				                title: '二月'
				            },{
				                title: '三月'
				            }, {
				                title: '四月'
				            },{
				                title: '五月'
				            }, {
				                title: '六月'
				            },{
				                title: '七月'
				            }, {
				                title: '八月'
				            },{
							    title: '九月'
							},{
							    title: '十月'
							}, {
							    title: '十一月'
							},{
							    title: '十二月'
							}
				        ],
				        active2:  this.doHandleMonth()-1,
				// tabList: ['一月','二月','三月', '四月', '五月', '六月','七月', '八月', '九月', '十月', '十一月','十二月'],
				opCard_stic: this.doHandleMonth()-1, // 默认月份 index
				filter: false,
				Activitylist:[],
				sortType: ['默认排序', '最新发布'], //排序
				sort_stic:0,//排序index
				fee_stic:0,//费用index
				sta_stic:'',//状态index
				clss_stic:'',//分类index
				feeList: ['全部', '免费', '收费'], //费用
				stuasList: ['未开始', '报名中', '已结束'], //状态
				classifyList: [] ,//分类
				monthId:this.doHandleMonth(),//月份id
				feeId:0,//费用id
			    status:'',//状态id
				type:'',//type
				id_sort:'asc',//排序
				bannerList:"",
				token:"",
				tokenShow:false,
			userInfo:"",
			store:""
			}
		},
		 watch:{
		        active2(index) {
		            console.log(this.active2)  // 0
					this.monthId = index+1;
					this.page = 1;
					this.getActivityList(true);
		        }
		    },
		onLoad(options){
			this.store = options.store;
			console.log(options)
			// this.page = 1;
			// this.weetlist = [];
			// this.getActivityList();
			// this.getBanner()
		},
		onShow(){
			this.token = uni.getStorageSync('accessToken');
			// this.doHandleMonth()
			this.Activitylist = [];
			this.page = 1;
			if(this.Activitylist==''){
				this.getActivityList();
			}
			this.getBanner();
		
		},
		components: {uniLoadMore,msTabs},
		
		onTabItemTap(item) {
		   this.store = 0;
		     this.Activitylist = [];
		     this.page = 1;
		     if(this.Activitylist==''){
		     	this.getActivityList();
		     }
		  },
		onReachBottom () {
			if (this.loadStatus === 'more') {
				this.getActivityList()
			}
		},
		methods: {
			//暂不登录
			dontLogin(){
				this.tokenShow = false
			},
			hanldHomefiltr() {
				this.filter = true
			},
			hanldHomeClos() {
				this.filter = false
			},
			toDetails(item) {
				if(this.token==''){
					this.tokenShow = true;
				}
				if(this.token){
					let aa = item.id;
					// let aa = item;
					// let bb = JSON.stringify(aa)
					// let cc = encodeURIComponent(bb);
					uni.navigateTo({url:`/pages/find/activityInfo?address=${aa}`})
				}
			},
		
			//选择排序
			hanldsortType(index){
				this.sort_stic = index;
				if(index==0){
					this.id_sort = 'asc'
				}
				if(index==1){
					this.id_sort = 'desc'
				}
				this.page = 1;
			},
			//选择费用
			hanldfee(index){
				this.fee_stic = index;
				this.feeId = index;
				this.page = 1;
			},
			//选择状态
			hanldstatus(index){
				this.sta_stic = index;
				this.status = index+1;
				this.page = 1;
			},
			//选择分类
			hanldClassify(item,index){
				this.clss_stic = item.id;
				this.type = item.id;
				this.page = 1;
			},
			//重置
			hanldNo(type){
				this.page = 1;
				this.id_sort = 'asc';
				this.sort_stic = 0;//排序index
				this.fee_stic = 0;//费用index
				this.sta_stic = '';//状态index
				this.clss_stic = '';//分类index
				this.monthId = '1';//月份id
				this.feeId = 0;//费用id
				this.status = '';//状态id
				this.type = '';//type
				this.active2 = 0; // 选中状态
				this.getActivityList(true)
			},
			//确定
			hanldOk(type){
				this.page = 1;
				this.getActivityList(true)
			},
			//活动列表
			getActivityList(type) {
				const params = {
					month: this.monthId,
					page: this.page,
					fee:this.feeId,
					status: this.status,
					type: this.type,
					id_sort:this.id_sort,
					store:this.store
					
				}

				this.loadStatus = 'loading'
				this.$http.get('/activity/list', {
					params: params
				}).then((response) => {
					const res = response.data;
					console.log(res)
					if (res.code == 0) {
						if (this.page > res.data.list.last_page) {
							// 没有数据了
							this.loadStatus = 'noMore'
						} else {
							// 未加载完毕
							this.loadStatus = 'more'
							this.page++
						}
						console.log(res)
						if (type) {
							this.Activitylist = res.data.list.data
						} else {
							this.Activitylist = [...this.Activitylist, ...res.data.list.data];
							this.classifyList = res.data.type;
							this.time  = res.time;
							console.log(this.time)
						}
						this.filter = false
					} else {
						this.filter = false
					}
				})
			},
			getBanner() {
				const params = {
					type: 2
				}
				this.$http.get('/index/banner', {
					params: params
				}).then((response) => {
					const res = response.data;
					if(res.code==0){
						this.bannerList = res.data.list;
						console.log(this.bannerList)
					}
				})
			},
			
			//授权
			getLogin() {

				var that = this;
				wx.login({
					//获取code 使用wx.login得到的登陆凭证，用于换取openid==token代替
					success(res) {
						if (res.code) {
							console.log(res.code);
							that.code = res.code;
							//使用 wx.getSetting 获取用户当前的授权状态。
							wx.getSetting({
								success(res) {
									if (res.authSetting['scope.userInfo']) { //已授权=>getUserInfo
										wx.getUserInfo({
											success(res) {
												console.log(res)
												console.log("ressss")
												console.log(that.userinfo)
												
												const formData = {
													code: that.code,
													avatar:res.userInfo.avatarUrl,
													nickname:res.userInfo.nickName
												}
												that.$http.post('/login/wxLogin', formData).then((response) => {
													const res = response.data;
													console.log(res.data.token);
													if(res.data.token){
														that.$http.setConfig((config) => { /* 设置全局配置 */
														  config.header = {
														    ...config.header,
														    token: res.data.token,
														  }
														  return config
														})
														wx.setStorageSync("accessToken", res.data.token); //成功之后存入token
													}
													that.token = res.data.token;
													that.tokenShow = false;
													uni.showToast({title: res.msg, icon: 'none'});
												})
											},
											fail(err) {
												console.log(err)
											}
										})
									}
								}
							})
						} else {
							console.log("登录失败！" + res.errMsg);
						}
					}
				});
			},
			doHandleMonth() {
			    var myDate = new Date();
			    var tMonth = myDate.getMonth();
			 
			    var m = tMonth + 1;
			  //   if (m.toString().length == 1) {
			  //       m = "0" + m;
					console.log(m+"月份")
			  //   }
				return m;
			}
		}

	}
</script>

<style scoped>
	.swiper {
		width: 100%;
		overflow: hidden;
		height: 180px;
	}
.home-wrapper>>>.uni-load-more__text{
	margin-top: 100px;
}
	.home-wrapper>>>uni-swiper .uni-swiper-wrapper {
		z-index: 99;
	}
	.home-wrapper>>>.tabBlock .tab{
		    height: 35px;
			padding-bottom: 0 !important;
			padding-top: 5px;
	}

	.swiper .img {
		height: 180px;
		width: 100%;
	}

	.home-wrapper {
		width: 100%;
	}

	.find_benber {
		height: 180px;
		width: 100%;
	}

	.find_benber image {
		height: 100%;
		width: 100%;
	}

	.home-header {
		display: flex;
		flex-direction: row;
		background-color: #fff;
		width: 100%;
		z-index: 999;

		/*  #ifdef  H5  */
		top: 224px;
		/*  #endif  */

		/*  #ifdef  MP-WEIXIN  */
		top: 181px;
		/*  #endif  */



	}
	.home-wrapper>>>.tabBlock{
		width: 90%;
	}

	.active {
		/* display: -webkit-box;
		display: -webkit-flex;
		-webkit-box-orient: horizontal;
		-webkit-box-direction: normal;
		-webkit-flex-direction: row; */
		/* flex-direction: row; */
		background-color: #fff;
		/* width: 83%; */
		white-space: nowrap;
		/* align-items: center; */
		position: relative;
	}
.fill-img{
	position: absolute;
	    right: 7px;
	    top: 7px;
	    background-color: #fff;
}
	.active-item {
		padding: 0 18px;
		font-size: 15px;
		color: #333333;
	}

	.home-fill image {
		width: 18px;
		height: 18px;
	}

	.home-fill {
		background: linear-gradient(83deg, rgba(238, 238, 238, 0) 0%, rgba(0, 0, 0, 0.08) 100%);
		width: 16px;
		    position: absolute;
		    right: 30px;
		    top: 0;
		    height: 35px;
	}

	.home {
		height: 115px;
		background-color: #FFFFFF;
		margin: 10px 17px;
		padding: 10px;
		border-radius: 5px;
		display: flex;
		flex-direction: row;
		align-items: center;
		position: relative;
		/* top: 32px; */
	}

	.home-img image {
		width: 95px;
		height: 95px;
	}

	.home-img {
		margin-right: 10px;
	}

	.home-add {
		display: flex;
		flex-direction: row;
		justify-content: space-between;
		align-items: center;
	}

	.home-list {
		width: 100%;
	}

	.home-book {
		height: 20px;
		border-radius: 3px;
		color: #999999;
		font-size: 10px;
		text-align: left;
		line-height: 21px;
	}

	.home-photo {
		display: flex;
		flex-direction: row;
	}

	.home-photo image {

		/*  #ifdef  H5  */
		width: 25px;
		height: 25px;
		/*  #endif  */

		/*  #ifdef  MP-WEIXIN */
		width: 20px;
		height: 20px;
		/*  #endif  */

		border-style: solid;
		border-color: #fff;
		border-radius: 50%;
		margin: 0px -40upx;
	}

	.home-stus {
		width: 39px;
		height: 39px;
		position: absolute;
		right: 0;
		top: 0;
	}

	.screen {
		height: 70%;
		background-color: #fff;
		position: fixed;
		/*  #ifdef  H5 */
		top: 223px;
		/*  #endif  */

		/*  #ifdef  MP-WEIXIN */
		bottom: 0px;
		/*  #endif  */

		width: 100%;
		display: flex;
		flex-direction: column;
		z-index: 99;
	}
 /* 定义滚动条样式 */
/* ::-webkit-scrollbar{width:14px;height: 10px;}
::-webkit-scrollbar-track{background-color:#bee1eb;}
::-webkit-scrollbar-thumb{background-color:#00aff0;}
::-webkit-scrollbar-thumb:hover {background-color:#9c3}
::-webkit-scrollbar-thumb:active {background-color:#00aff0}
 */
	.screen-text {
		display: flex;
		flex-direction: row;
		flex-wrap: wrap;
	}

	.screen-con {
		height: 34px;
		border-radius: 5px;
		border: 1px solid #E7E7E7;
		font-size: 15px;
		line-height: 34px;
		text-align: center;
		color: #333333;
		margin-top: 9px;
		margin-right: 14upx;
		padding: 0 58upx;
	}

	.screen-title {
		padding: 14px 0 0;
		font-size: 14px;
	}

	.search-no {
		width: 50%;
		height: 44px;
		background: rgba(219, 219, 219, 1);
		text-align: center;
		line-height: 44px;
		font-size: 15px;
		color: #fff;
	}

	.search-ok {
		width: 50%;
		height: 44px;
		background: rgba(255, 95, 73, 1);
		text-align: center;
		line-height: 44px;
		font-size: 15px;
		color: #fff;
	}

	.screen-but {
		position: absolute;
		bottom: 0;
		display: flex;
		flex-direction: row;
		width: 100%;
	}

	.screen-bg {
		position: absolute;
		width: 100%;
		height: 100%;
		background: rgba(0, 0, 0, 0.6);
		top: 0;
	}

	.sticStyle {
		font-size: 18px;
		color: #FF5F49;
		font-weight: bold;
	}
	.typeStyle{
			color: #FF5F49 !important;
			border: 1px solid  #FF5F49 !important
		}
		
		/*授权*/
		.dialog-mask {
			position: fixed;
			z-index: 1000;
			top: 0;
			right: 0;
			left: 0;
			bottom: 0;
			background: rgba(0, 0, 0, 0.3);
		}
		
		.dialog-info {
			position: fixed;
			z-index: 5000;
			width: 80%;
			max-width: 600rpx;
			top: 50%;
			left: 50%;
			-webkit-transform: translate(-50%, -50%);
			transform: translate(-50%, -50%);
			background-color: #ffffff;
			text-align: center;
			overflow: hidden;
		}
		
		.dialog-title {
			font-size: 36rpx;
			padding: 30rpx 30rpx 10rpx;
		}
		
		.dialog-content {
			padding: 10rpx 30rpx 20rpx;
			min-height: 80rpx;
			font-size: 32rpx;
			line-height: 1.3;
			word-wrap: break-word;
			word-break: break-all;
			color: #999999;
		}
		
		.dialog-footer {
			display: flex;
			align-items: center;
			position: relative;
			line-height: 90rpx;
			font-size: 34rpx;
		}
		
		.dialog-btn {
			display: block;
			-webkit-flex: 1;
			flex: 1;
			position: relative;
			font-size: 16px;
		}
		
		button {
			background-color: #fff;
			border-radius: 0;
		}
		
		button::after {
			border-radius: 0;
		}
</style>
